<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>所有文章</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="searchValue" @keyup.enter="searchOn">
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="searchOn">搜索文章</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table
        :data="articlelist"
        border
        stripe
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column label="文章标题" prop="title"></el-table-column>
        <el-table-column label="文章类别" prop="category"></el-table-column>
        <el-table-column label="添加日期" prop="addTime"></el-table-column>
        <el-table-column label="内容介绍" prop="introduce"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" @click="edit(scope.row._id)" icon="el-icon-edit" size="mini"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" @click="Del(scope.row._id)" icon="el-icon-delete" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      articlelist: [],
      loading: true,
      searchValue: ''
    };
  },
  created() {
    this.getArticle();
  },
  methods: {
    getArticle() {
      this.$axios({
        method: "get",
        url: "/articles",
      }).then((response) => {
        console.log(response.data.data);
        if (!response.data.data) {
          this.loading = false;
          return false;
        }
        response.data.data.forEach((item, index) => {
          setTimeout(() => {
            item.addTime = item.addTime.substring(0, 10);
            this.articlelist.push(item);
            this.loading = false;
          }, 2000);
        });
      });
      this.loading = true;
    },
    edit(id) {
      console.log(id);
      this.$router.push("/admin/revise-articles/" + id);
    },
    Del(id) {
      console.log(id);
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            method: "post",
            url: "/deleteArticles",
            data: {
              _id: id,
            },
          }).then((res) => {
            console.log(res);
            this.articlelist = [];
            this.getArticle();
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    searchOn(){
      this.$axios({
        method: 'post',
        url: '/search',
        data: {
          data: this.searchValue
        }
      }).then((res)=>{
        console.log(res.data.data)
        this.searchValue = ''
        this.articlelist = []
        res.data.data.forEach(item=>{
          item.addTime = item.addTime.substring(0, 10);
          this.articlelist.push(item)
        })
      })
    }
  },
};
</script>